package com.glodon.ggzy.core.utils;

import com.glodon.ggzy.sso.utils.CryptString;
import com.glodon.ggzy.sso.utils.CryptUtil;
import com.glodon.ggzy.sso.utils.TimeUtil;
import com.glodon.ggzy.webservice.ObjectFactory;
import com.glodon.ggzy.webservice.QueryTicketInfoRequest;
import com.glodon.ggzy.webservice.QueryTicketInfoResponse;
import com.glodon.ggzy.webservice.UaService;
import com.glodon.ggzy.webservice.UaWebServiceSoap;
import com.glodon.ggzy.webservice.Unit;

/**
 * 
 * 
 * SOO工具类
 * @author zhangxj
 * @since jdk1.6
 * @date 2013-6-17
 *
 */
public class SSOUtils {

	public static Unit getUser(String passportLoginResponse) throws Exception {

			String resultCode = "";
			String[] src = passportLoginResponse.split("\\$");
			String desSsDeviceNo = src[0];
			String deCryptString = CryptString.getDecryptString(src[1], CustomizedPropertyPlaceholderConfigurer.getProperty(Constants.SYSTEM_XIANGLIANG),
					CustomizedPropertyPlaceholderConfigurer.getProperty(Constants.KEY));
			String[] deCryptArray = deCryptString.split("\\$");
			String digestString = "";
			String ticket = "";
			for (int i = 0; i < deCryptArray.length - 1; i++) {
				digestString += deCryptArray[i];
				if (i == 0) {
					resultCode = deCryptArray[i];
					digestString += desSsDeviceNo;
				}
				if (i == 1) {
					ticket = deCryptArray[i]; //获取返回的ticket值
				}
			}
			//调用webservice
			ObjectFactory objectFactory = new ObjectFactory();
			UaService uaService = new UaService();
			UaWebServiceSoap uaWebServiceSoap = uaService.getUaWebServiceSoap();
			QueryTicketInfoRequest queryTicketInfoRequest = objectFactory.createQueryTicketInfoRequest();
			queryTicketInfoRequest.setClientId(CustomizedPropertyPlaceholderConfigurer.getProperty(Constants.CLIENT_ID));
			queryTicketInfoRequest.setTicket(ticket);
			String time = TimeUtil.getNowTime();
			queryTicketInfoRequest.setTimeStamp(time);
			String str = queryTicketInfoRequest.getClientId() + queryTicketInfoRequest.getTicket() + queryTicketInfoRequest.getTimeStamp();
			String sign = CryptUtil.udb3DesCrypt(str, CustomizedPropertyPlaceholderConfigurer.getProperty(Constants.KEY), CustomizedPropertyPlaceholderConfigurer.getProperty(Constants.SYSTEM_XIANGLIANG));
			queryTicketInfoRequest.setSign(sign);

			QueryTicketInfoResponse queryTicketInfo = uaWebServiceSoap.queryTicketInfo(queryTicketInfoRequest);

			Integer accountType = queryTicketInfo.getAccountType();

			if (accountType != null && Constants.Person_User == accountType) {

				return null;
			}

			if (accountType != null && Constants.Enterprise_User == accountType) {

				return queryTicketInfo.getUnit();
			}


		return null;
	}
}
